<template>
  <div class="app-container">
    <el-card class="search-card">
      <div slot="header" class="clearfix">
        <span>筛选条件</span>
        <el-button style="float: right; padding: 3px 0" type="text" @click="toggleSearch">收起/展开</el-button>
      </div>
      <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="100px">
        <el-form-item label="打印机序列号" prop="serialNumber">
          <el-input
            v-model="queryParams.serialNumber"
            placeholder="请输入打印机序列号"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="产品型号" prop="productModel">
          <el-input
            v-model="queryParams.productModel"
            placeholder="请输入产品型号"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="生产日期" prop="manufactureDate">
          <el-date-picker clearable
            v-model="queryParams.manufactureDate"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择生产日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
            <el-option label="启用" value="1"></el-option>
            <el-option label="禁用" value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card class="operation-card">
      <div slot="header" class="clearfix">
        <span>操作面板</span>
      </div>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['nongjt:serial:add']"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            icon="Edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['nongjt:serial:edit']"
          >修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['nongjt:serial:remove']"
          >删除</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            icon="Download"
            @click="handleExport"
            v-hasPermi="['nongjt:serial:export']"
          >导出</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="info"
            icon="Upload"
            @click="handleImport"
            v-hasPermi="['nongjt:serial:import']"
          >导入</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            icon="Refresh"
            @click="handleSync"
          >同步</el-button>
        </el-col>
        <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
    </el-card>

    <el-card class="table-card">
      <div slot="header" class="clearfix">
        <span>打印机序列号库列表</span>
        <span class="total-count">总数：{{ total }}</span>
      </div>
      <el-table 
        v-loading="loading" 
        :data="serialList" 
        @selection-change="handleSelectionChange"
        border
        highlight-current-row
        :row-class-name="tableRowClassName"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="序列号ID" align="center" prop="serialId" width="100" />
        <el-table-column label="打印机序列号" align="center" prop="serialNumber" show-overflow-tooltip>
          <template #default="scope">
            <el-link type="primary" @click="copyToClipboard(scope.row.serialNumber)">{{ scope.row.serialNumber }}</el-link>
          </template>
        </el-table-column>
        <el-table-column label="产品型号" align="center" prop="productModel" show-overflow-tooltip />
        <el-table-column label="生产日期" align="center" prop="manufactureDate" width="120">
          <template #default="scope">
            <span>{{ parseTime(scope.row.manufactureDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center" prop="status" width="80">
          <template #default="scope">
            <el-tag :type="scope.row.status === '1' ? 'success' : 'danger'">
              {{ scope.row.status === '1' ? '启用' : '禁用' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center" prop="createTime" width="160">
          <template #default="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="备注" align="center" prop="remark" show-overflow-tooltip />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
          <template #default="scope">
            <el-button size="small" type="text" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['nongjt:serial:edit']">修改</el-button>
            <el-button size="small" type="text" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['nongjt:serial:remove']">删除</el-button>
            <el-button size="small" type="text" icon="View" @click="handleDetail(scope.row)">详情</el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>

    <!-- 添加或修改打印机序列号库对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form ref="serialRef" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="打印机序列号" prop="serialNumber">
              <el-input v-model="form.serialNumber" placeholder="请输入打印机序列号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="产品型号" prop="productModel">
              <el-input v-model="form.productModel" placeholder="请输入产品型号" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="生产日期" prop="manufactureDate">
              <el-date-picker clearable
                v-model="form.manufactureDate"
                type="date"
                value-format="YYYY-MM-DD"
                placeholder="请选择生产日期"
                style="width: 100%">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态" prop="status">
              <el-radio-group v-model="form.status">
                <el-radio label="1">启用</el-radio>
                <el-radio label="0">禁用</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 详情对话框 -->
    <el-dialog title="打印机序列号详情" v-model="detailOpen" width="600px" append-to-body>
      <el-descriptions :column="1" border>
        <el-descriptions-item label="序列号ID">{{ detailData.serialId }}</el-descriptions-item>
        <el-descriptions-item label="打印机序列号">{{ detailData.serialNumber }}</el-descriptions-item>
        <el-descriptions-item label="产品型号">{{ detailData.productModel }}</el-descriptions-item>
        <el-descriptions-item label="生产日期">{{ parseTime(detailData.manufactureDate, '{y}-{m}-{d}') }}</el-descriptions-item>
        <el-descriptions-item label="状态">
          <el-tag :type="detailData.status === '1' ? 'success' : 'danger'">
            {{ detailData.status === '1' ? '启用' : '禁用' }}
          </el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="备注">{{ detailData.remark }}</el-descriptions-item>
        <el-descriptions-item label="创建人">{{ detailData.createBy }}</el-descriptions-item>
        <el-descriptions-item label="创建时间">{{ parseTime(detailData.createTime) }}</el-descriptions-item>
        <el-descriptions-item label="更新人">{{ detailData.updateBy }}</el-descriptions-item>
        <el-descriptions-item label="更新时间">{{ parseTime(detailData.updateTime) }}</el-descriptions-item>
      </el-descriptions>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="detailOpen = false">关 闭</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Serial">
import { listSerial, getSerial, delSerial, addSerial, updateSerial } from "@/api/nongjt/serial"

const { proxy } = getCurrentInstance()

const serialList = ref([])
const open = ref(false)
const detailOpen = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")
const detailData = ref({})

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    serialNumber: null,
    productModel: null,
    manufactureDate: null,
    status: null,
  },
  rules: {
    serialNumber: [
      { required: true, message: "打印机序列号不能为空", trigger: "blur" }
    ],
    productModel: [
      { required: true, message: "产品型号不能为空", trigger: "blur" }
    ],
    status: [
      { required: true, message: "状态不能为空", trigger: "change" }
    ],
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询打印机序列号库列表 */
function getList() {
  loading.value = true
  listSerial(queryParams.value).then(response => {
    serialList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    serialId: null,
    serialNumber: null,
    productModel: null,
    manufactureDate: null,
    status: "1",
    remark: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  }
  proxy.resetForm("serialRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.serialId)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加打印机序列号库"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _serialId = row.serialId || ids.value
  getSerial(_serialId).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改打印机序列号库"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["serialRef"].validate(valid => {
    if (valid) {
      if (form.value.serialId != null) {
        updateSerial(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addSerial(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _serialIds = row.serialId || ids.value
  proxy.$modal.confirm('是否确认删除打印机序列号库编号为"' + _serialIds + '"的数据项？').then(function() {
    return delSerial(_serialIds)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('nongjt/serial/export', {
    ...queryParams.value
  }, `serial_${new Date().getTime()}.xlsx`)
}

/** 导入按钮操作 */
function handleImport() {
  proxy.$modal.msgWarning("导入功能待开发")
}

/** 同步按钮操作 */
function handleSync() {
  proxy.$modal.msgWarning("同步功能待开发")
}

/** 详情按钮操作 */
function handleDetail(row) {
  detailData.value = row
  detailOpen.value = true
}

/** 切换搜索栏显示状态 */
function toggleSearch() {
  showSearch.value = !showSearch.value
}

/** 表格行样式 */
function tableRowClassName({ row, rowIndex }) {
  if (row.status === '0') {
    return 'warning-row'
  }
  return ''
}

/** 复制到剪贴板 */
function copyToClipboard(text) {
  navigator.clipboard.writeText(text).then(() => {
    proxy.$modal.msgSuccess("已复制到剪贴板")
  }).catch(() => {
    proxy.$modal.msgError("复制失败")
  })
}

getList()
</script>

<style scoped>
.app-container {
  background-color: #f0f2f5;
  padding: 20px;
}

.search-card,
.operation-card,
.table-card {
  margin-bottom: 20px;
}

.total-count {
  float: right;
  margin-right: 20px;
  font-weight: normal;
  color: #606266;
}

:deep(.el-table .warning-row) {
  background-color: #fdf6ec;
}

:deep(.el-table .cell) {
  padding: 0 5px;
}
</style>